<script setup>
import { reactive, toRefs, ref, computed, onMounted } from 'vue';
import { ElInput, ElMessage, ElOption, ElSelect, ElTable, ElTableColumn, ElTag } from 'element-plus';
import { delSystemLog, postSystemLogPage } from '@/apis/api-system';
import { OPERATION_TYPE } from '@/common/common-const';
import PaginationIndex from '@/components/PaginationIndex.vue';
import ReleasePromptMessage from '@/components/ReleasePromptMessage.vue';

const props = defineProps({
  name: {
    type: String,
  },
});
const { name } = toRefs(props);

const titleName = computed(() => name.value);
const total = ref(0);
const selectParams = reactive({
  pageNumber: 1,
  pageSize: 13,
  title: '',
  seedlingName: '',
  businessType: null,
  status: null,
});

const tableData = ref([]);

const messageInfo = reactive({
  messageBoxShow: false,
  messageText: '',
  messageBoxType: '',
});

// 操作
const handleOperation = (type, item) => {
  if (type == OPERATION_TYPE.DELETE) {
    messageInfo.infoBoxValue = item;
    messageInfo.messageBoxType = '删除';
    messageInfo.messageBoxShow = true;
    messageInfo.messageText = '该数据删除后不可恢复，确定要删除吗？';
  }
};

const handleMessageBox = (val) => {
  messageInfo.messageBoxShow = false;
  messageInfo.messageText = '';

  if (val) {
    //  删除接口
    delSystemLogApi(messageInfo.infoBoxValue.operId);
  }

  messageInfo.messageBoxType = '';
};

// 分页
const handleCurrentChange = (val) => {
  selectParams.pageNumber = val;
  postSystemLogPageApi(selectParams);
};

onMounted(() => {
  postSystemLogPageApi(selectParams);
});

const handleReset = () => {
  selectParams.pageNumber = 1;
  selectParams.title = '';
  selectParams.seedlingName = '';
  selectParams.businessType = null;
  selectParams.status = null;
  postSystemLogPageApi(selectParams);
};

const handleSelect = () => {
  postSystemLogPageApi(selectParams);
};

/*
 *@description: 分页查询系统日志
 *@author:chen_fei
 *@Date: 2024-06-14 17:43:38
 */
const postSystemLogPageApi = async (val) => {
  const [err, data] = await postSystemLogPage(val);

  if (!err) {
    tableData.value = data.data.records;
    total.value = data.data.total;
  }
};

/*
 *@description: 删除日志
 *@author:chen_fei
 *@Date: 2024-06-14 17:43:03
 */
const delSystemLogApi = async (id) => {
  const [err] = await delSystemLog(id);

  if (!err) {
    ElMessage.success('删除成功!');
  }

  messageInfo.infoBoxValue = '';
  postSystemLogPageApi(selectParams);
};
</script>
<template>
  <div class="root h-100% w-100% rounded-5px p-1.5rem">
    <h3>{{ titleName }}</h3>
    <div class="flex-x-between">
      <div class="select_top mt-20px flex-center">
        <div class="select_u flex-center">
          <div class="flex-center">
            <span>模块标题</span>
            <ElInput v-model="selectParams.title" placeholder="请输入" />
          </div>
          <div class="flex-center">
            <span>操作状态</span>
            <ElSelect v-model="selectParams.status" placeholder="请选择">
              <ElOption label="正常" :value="0" />
              <ElOption label="异常" :value="1" />
            </ElSelect>
          </div>
          <div class="flex-center">
            <span>业务类型</span>
            <ElSelect v-model="selectParams.businessType" placeholder="请选择">
              <ElOption label="新增" :value="1" />
              <ElOption label="修改" :value="2" />
              <ElOption label="删除" :value="3" />
              <ElOption label="查询" :value="4" />
              <ElOption label="其他" :value="0" />
            </ElSelect>
          </div>
        </div>
      </div>
      <div class="mt-20px flex-center">
        <div class="select_btn mr-20px flex-center">
          <div @click="handleReset()">重置</div>
          <div @click="handleSelect()">查询</div>
        </div>
      </div>
    </div>
    <div class="mt-20px">
      <ElTable
        :data="tableData"
        style="width: 100%"
        border
        row-key="id"
        :header-cell-style="{
          color: '#333333',
          background: 'rgba(97, 186, 55, 0.06)',
        }"
      >
        <ElTableColumn prop="operId" label="日志编号" align="center" />
        <ElTableColumn prop="title" label="模块标题" align="center" show-overflow-tooltip />
        <ElTableColumn prop="businessType" label="操作类型" align="center" show-overflow-tooltip>
          <template #default="scope">
            <ElTag
              size="large"
              :type="
                scope.row?.businessType == 1
                  ? 'warning'
                  : scope.row?.businessType == 2
                  ? 'danger'
                  : scope.row?.businessType == 3
                  ? 'success'
                  : 'info'
              "
              >{{
                scope.row?.businessType == 1
                  ? '新增'
                  : scope.row?.businessType == 2
                  ? '修改'
                  : scope.row?.businessType == 3
                  ? '删除'
                  : scope.row?.businessType == 4
                  ? '查询'
                  : '其他'
              }}</ElTag
            >
          </template>
        </ElTableColumn>
        <ElTableColumn prop="method" label="方法名称" align="center" show-overflow-tooltip />
        <ElTableColumn prop="requestMethod" label="请求方式" align="center" show-overflow-tooltip />
        <ElTableColumn prop="operName" label="操作人员" align="center" show-overflow-tooltip />

        <ElTableColumn prop="operIp" label="主机地址" align="center" show-overflow-tooltip />
        <ElTableColumn prop="operLocation" label="操作地点" align="center" show-overflow-tooltip />
        <ElTableColumn prop="status" label="操作状态" align="center" show-overflow-tooltip>
          <template #default="scope">
            <ElTag :type="scope.row?.status == 0 ? 'warning' : 'danger'" size="large">{{
              scope.row?.status == 0 ? '成功' : '失败'
            }}</ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn prop="errorMsg" label="错误信息" align="center" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row?.errorMsg ? scope.row?.errorMsg : '--' }}
          </template>
        </ElTableColumn>

        <ElTableColumn prop="operTime" label="操作时间" align="center" show-overflow-tooltip sortable />
        <ElTableColumn prop="costTime" label="消耗时间" align="center" show-overflow-tooltip sortable>
          <template #default="scope">
            <span>{{ scope.row?.costTime ? `${scope.row?.costTime}毫秒` : '--' }}</span>
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作" align="center">
          <template #default="scope">
            <div class="flex items-center justify-around">
              <div
                class="del_btn h-35px w-75px flex-center cursor-pointer"
                @click="handleOperation('DELETE', scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </ElTableColumn>
      </ElTable>
    </div>
    <PaginationIndex
      :page-number="selectParams.pageNumber"
      :page-size="selectParams.pageSize"
      :total="total"
      @handle-current-change="handleCurrentChange"
    />
  </div>

  <!-- 提示 -->
  <ReleasePromptMessage
    v-if="messageInfo.messageBoxShow"
    :message-box-show="messageInfo.messageBoxShow"
    :message-text="messageInfo.messageText"
    :message-box-type="messageInfo.messageBoxType"
    @handle-message-box="handleMessageBox"
  />
</template>

<style scoped lang="scss">
.root {
  font-size: $base-system-font-size-base;
  background-color: $base-background-white;

  .select_top {
    .select_u {
      display: flex;
      white-space: nowrap;
      div {
        display: flex;
        align-items: center;
        margin-right: 20px;

        span {
          display: inline-block;
          margin-right: 12px;
        }
      }
    }
  }
  .select_btn {
    display: flex;
    div {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100px;
      height: 40px;
      font-size: 16px;
      border-radius: 4px;
      cursor: pointer;

      &:first-child {
        margin-right: 16px;
        color: $base-color-info;
        background: $base-button-background-info;
      }

      &:nth-child(2) {
        color: $base-button-color-green;
        background: $base-button-background-green;
      }
    }
  }

  .del_btn {
    font-size: $base-system-font-size-base;
    border-radius: 5px;
  }

  .del_btn {
    color: $base-button-color-red;
    background-color: $base-button-background-red;
  }
}

:deep(.el-input .el-input__wrapper) {
  width: 240px;
  height: 40px;
}
:deep(.el-select__wrapper) {
  width: 240px;
  height: 40px;
}
</style>
